//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by 张子国 on 15/11/15.
//  Copyright © 2015年 张子国. All rights reserved.
//

import UIKit
import SnapKit
let StatusCellMargin: CGFloat = 12
let StatusCellIconWitdth: CGFloat = 35

class StatusCell: UITableViewCell {
    
    //原创微博底部约束  用来记录底部约束  后续根据是否有配图 来动态修改
    var bottomConstraints: Constraint?

    //数据模型
    var status: Status? {
        didSet {
            originalView.status = status
            //移除约束
            self.bottomConstraints?.uninstall()
            //设置转发微博的模型
            if let ret = status?.retweeted_status {
            //设置数据源
                reweetedView.reweetedStatus = ret
            //设置显示
                reweetedView.hidden = false
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(reweetedView.snp_bottom).constraint
                })
            } else {
                
                //设置隐藏
                reweetedView.hidden = true
                bottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.bottomConstraints = make.top.equalTo(originalView.snp_bottom).constraint
                })

                
            }
        }
    }


    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .None
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 设置UI
    private func setupUI () {
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(reweetedView)
      
        //布局
        //原创视图
        originalView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            make.left.equalTo(contentView.snp_left)
            
        //转发微博视图
        reweetedView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.left.equalTo(originalView.snp_left)
            make.right.equalTo(originalView.snp_right)
        })
       
        //底部视图
        bottomView.snp_makeConstraints(closure: { (make) -> Void in
        self.bottomConstraints = make.top.equalTo(reweetedView.snp_bottom).offset(StatusCellMargin).constraint
        make.left.equalTo(contentView.snp_left)
        make.right.equalTo(contentView.snp_right)
        make.height.equalTo(40)
        })
            
        
        contentView.snp_makeConstraints(closure: { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            make.bottom.equalTo(bottomView.snp_bottom)
            
            
        })

        }
        
    }
    //MARK: 懒加载空间
    private lazy var originalView : StatusOriginalView = StatusOriginalView()
    private lazy var bottomView : StatusCellBottomView = StatusCellBottomView()
    private lazy var reweetedView : ReweetedStatusView = ReweetedStatusView()
}